package com.example.fragmentaddviewpager.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.fragmentaddviewpager.R;

import java.util.ArrayList;
import java.util.List;

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyHolder> {
    private List<String> datas =new ArrayList<String>();
    private List<Integer> colors =new ArrayList<Integer>();
    public MyAdapter(){
        for (int i=1;i<10;i++){
            datas.add("hello"+i);
        }
        colors.add(R.color.white);
        colors.add(R.color.red);
        colors.add(R.color.black);
        colors.add(R.color.yellow);
        colors.add(R.color.purple_200);
        colors.add(R.color.purple_500);
        colors.add(R.color.purple_700);
        colors.add(R.color.white);
        colors.add(R.color.red);
    }
    //把页面展示到viewpager里面
    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.page_item, parent, false); //解析xml文件
        return new MyHolder(view);
    }

    //绑定数据，就是各个界面的数据切换
    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {
        holder.item_tv1.setText(datas.get(position));
        holder.mcontainer.setBackgroundResource(colors.get(position));

    }

    //滚动页面的个数
    @Override
    public int getItemCount() {
        return datas.size();
    }

    class MyHolder extends RecyclerView.ViewHolder {
        TextView item_tv1;
        RelativeLayout mcontainer;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            mcontainer = itemView.findViewById(R.id.container);
            item_tv1 = itemView.findViewById(R.id.item_tv1);
        }
    }
}
